package communication.server.vertx.handler.netmodule;

import communication.common.business.base.CommonBaseHandler;
import communication.common.business.base.FcrSocketAddress;
import communication.common.business.base.MethodMap;
import communication.common.business.base.WorkModeEnum;
import communication.common.business.hole.DefaultHoleHandler;
import communication.common.business.hole.P2pHoleRequest;
import communication.common.business.hole.ServerNetService;
import communication.common.business.heartbeat.HeartBeat;
import communication.common.business.heartbeat.HeartBeatServiceOfServer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * 处理网络
 * @author mac
 */
@Slf4j
@Component
public class ServerNetModuleHandler extends CommonBaseHandler {

    @Autowired
    private ServerNetService netService;

    @Autowired
    private HeartBeatServiceOfServer heartBeatServiceOfServer;

    @Autowired
    private DefaultHoleHandler defaultHoleHandler;


    @PostConstruct
    private void init() throws Exception {
        // 转发打洞请求
        MethodMap transferHole=MethodMap.builder()
                .op("hole")
                .serviceBean(netService)
                .methodName("hole")
                .type(0)
                .requestClassList(new Class[]{P2pHoleRequest.class, FcrSocketAddress.class})
                .responseClass(Boolean.class)
                .build();

        addMethodMap(transferHole);

        // 处理心跳
        MethodMap heartBeat=MethodMap.builder()
                .op("heartBeat")
                .serviceBean(heartBeatServiceOfServer)
                .methodName("handleHeartBeat")
                .type(0)
                .requestClassList(new Class[]{HeartBeat.class,FcrSocketAddress.class})
                .responseClass(null)
                .build();

        addMethodMap(heartBeat);


        // 处理打洞测试的请求
        MethodMap holeTest=MethodMap.builder()
                .op("test-hole")
                .serviceBean(defaultHoleHandler)
                .methodName("handleHoleTestReq")
                .type(0)
                .requestClassList(new Class[]{Integer.class, FcrSocketAddress.class})
                .responseClass(WorkModeEnum.class)
                .build();

        addMethodMap(holeTest);


        // 处理打洞测试请求的响应
        MethodMap holeTestResp=MethodMap.builder()
                .op("test-hole")
                .serviceBean(defaultHoleHandler)
                .methodName("handleHoleTestResp")
                .type(1)
                .requestClassList(new Class[]{WorkModeEnum.class, FcrSocketAddress.class})
                .responseClass(null)
                .build();

        addMethodMap(holeTestResp);

    }


    @Override
    public String getModuleName() {
        return "net";
    }
}
